<template>
    <div class='fxn-body'>
        <Searchs v-model:searchData="searchData" v-model:SearchForm="SearchForm" @handleSearch="handleSearch"></Searchs>
        <!-- Search End -->
        <div class="fxn-section fixed fxn-margin-t-20">
            <el-table v-loading="loading" :data="listData">
                <el-table-column label="案件名称" prop="title">
                    <template #default="scope">
                        <el-link type="primary" :underline="false" @click="handlePath(scope.row)">{{scope.row.title}}</el-link>
                    </template>
                </el-table-column>
                <el-table-column label="类别" prop="is_refund">
                    <template #default="scope">
                        {{scope.row.is_refund == 1 ? '收款' : '退款'}}
                    </template>
                </el-table-column>
                <el-table-column label="付款人" prop="drawee" />
                <el-table-column label="付款方式" prop="pay_method">
                    <template #default="scope">
                        {{$filter.formatData(scope.row.pay_method,'pay_method')}}
                    </template>
                </el-table-column>
                <el-table-column label="收/退款金额" prop="pay_money" />
                <el-table-column label="付款时间" prop="pay_time">
                    <template #default="scope">
                        {{$filter.dateTime(scope.row.pay_time)}}
                    </template>
                </el-table-column>
                <el-table-column label="扣除手续费" prop="fee" />
                <el-table-column label="银行流水号" prop="pay_number" />
                <el-table-column label="备注" prop="remark" />
                <el-table-column label="操作">
                    <template #default="scope">
                        <el-link v-if="authMenu('sys_finance_receipt_edit')" type="primary" :underline="false" @click="handleEdit(scope.row)">修改</el-link>
                        <el-link v-if="authMenu('sys_finance_receipt_refund')" type="primary" :underline="false" @click="handleRefund(scope.row)">退款</el-link>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <!-- Section End -->

        <div class="fxn-page fixed">
            <el-pagination v-model:current-page="page" v-model:page-size="perPage" :page-sizes="[10, 50, 100, 200, 500]" layout="total, sizes, prev, pager, next" :total="count" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
        </div>
        <!-- Page End -->

        <!-- 修改 -->
        <Collection v-model:opDialog="opDialog" :opData="opData" :editData="editData" @handleSubmit="handleSubmit"></Collection>
        <!-- 修改 End -->
    </div>
</template>

<script>
    import Searchs from '../Common/Search.vue'
    import Collection from './Common/Collection.vue'
    import { defineComponent, reactive, getCurrentInstance, onMounted, toRefs, computed } from 'vue'
    import { useRouter } from 'vue-router'
    import { useStore } from 'vuex'
    export default defineComponent({
        components: {Searchs,Collection},
        setup() {
            const { proxy } = getCurrentInstance();
            const router = useRouter();
            const store = useStore();

            //初始化数据
            const state = reactive({
                count: 0,
                page: 0,
                perPage: 10,
                loading: false,
                listData: [],

                //搜索
                SearchForm: {
                    title: '',
                    is_refund: '',
                    drawee: '',
                    pay_method: '',
                    pay_time: '',
                },
                searchData: [
                    {label: '案件名称', prop: 'title', type: 'input'},
                    {label: '类别', prop: 'is_refund', type: 'select', data: [{value: 1, label: '收款'},{value: 2, label: '退款'}], selectlabel: 'label', selectvalue: 'value'},
                    {label: '付款人', prop: 'drawee', type: 'input'},
                    {label: '收款方式', prop: 'pay_method', type: 'select', data: computed(() => store.state.configEnable.pay_method), selectlabel: 'value', selectvalue: 'name'},
                    {label: '收款时间', prop: 'pay_time', type: 'daterange'},
                ],

                //修改
                opDialog: false,
                opData: {
                    type: 2,
                    title: '修改',
                },
                editData: {},
            })

            //列表
            const getList = (page,perPage) => {
                state.loading = true;
                proxy.$http.get('/oa/Lawcase/financeDetail',{
                    page: page ? page : 1,
                    perPage: perPage ? perPage : 10,
                    title: state.SearchForm.title,
                    is_refund: state.SearchForm.is_refund,
                    drawee: state.SearchForm.drawee,
                    pay_method: state.SearchForm.pay_method,
                    pay_time: String(state.SearchForm.pay_time),
                })
                .then(res=> {
                    state.loading = false;
                    const { code, msg ,data } = res;
                    if(code != 1){
                        return proxy.$msg.error(msg);
                    }else{
                        state.count = data.count;
                        state.page = data.page;
                        state.perPage = data.perPage;
                        state.listData = data.list;
                    }
                })
            }

            //修改
            const handleEdit = (row) => {
                proxy.$http.get('/oa/Lawcase/financeDetailForm',{id: row.id})
                .then(res=> {
                    state.loading = false;
                    const { code, msg ,data } = res;
                    if(code != 1){
                        return proxy.$msg.error(msg);
                    }else{
                        state.opDialog = true;
                        state.editData = data;
                    }
                })
            }

            //提交修改
            const handleSubmit = () => {
                getList(state.page,state.perPage);
            }

            //退款
            const handleRefund = (row) => {
                proxy.$confirm("此操作会将该记录进行退款操作, 是否继续?", "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning",
                })
                .then(async () => {
                    proxy.$http.post('/oa/Lawcase/financeRefund',{id: row.id})
                    .then(res=> {
                        let { code, msg, data } = res;
                        if(code != 1){
                            return proxy.$msg.error(msg);
                        }else{
                            proxy.$msg.success('提交成功!');
                            getList(state.page,state.perPage);
                        }
                    })
                })
                .catch(() => {})
            }

            //跳转详情
            const handlePath = (row) => {
                router.push({path: '/CaseDetail', query: {id: row.lawcase_id, type: row.case_type, pathUrl: '/ReceiptManage'}})
            }

            //搜索
            const handleSearch = (e) => {
                state.SearchForm = e;
                getList(1,10);
            }

            //分页
            const handleSizeChange = (val) => {
                state.perPage = val;
                getList(state.page,state.perPage);
            }

            //页码
            const handleCurrentChange = (val) => {
                state.page = val;
                getList(state.page,state.perPage);
            }

            //获取权限
            const authMenu = (value) => {
                let currentRouter = router.currentRoute.value.path;
                let menuArr = (proxy.$public.authMenu(currentRouter).split(','));
                for (let index = 0; index < menuArr.length; index++) {
                    if(value == menuArr[index]){
                        return true
                    }
                }
            }
            
            //Dom加载
            onMounted(()=>{
                authMenu();
                getList();
            })
            
            return {
                ...toRefs(state),
                
                //请求方法
                getList,
                handleEdit,
                handleSubmit,
                handleRefund,
                handlePath,
                
                //组件方法
                authMenu,
                handleSearch,
                handleSizeChange,
                handleCurrentChange,
            }

        },
    })
</script>

<style scoped>
    
</style>